package com.example.baselibrary.wigits

import android.content.Context
import android.content.DialogInterface
import android.os.Handler
import android.os.Looper
import com.xuexiang.xui.utils.WidgetUtils
import com.xuexiang.xui.widget.dialog.LoadingDialog

/**
 * @author 郑
 * @date 2025/8/8.
 * qq：331758871
 * phone：18979397268
 */
class XLoadingDialogManger private constructor() {
    private var loadingDialog: LoadingDialog? = null
    private val mainHandler = Handler(Looper.getMainLooper())
    private val autoCloseRunnable = Runnable { hideLoading() }

    companion object {
        @Volatile
        private var instance: XLoadingDialogManger? = null
        private const val AUTO_CLOSE_DELAY = 1000 * 10L

        fun getInstance() = instance ?: synchronized(this) {
            instance ?: XLoadingDialogManger().also { instance = it }
        }
    }

    fun showLoading(
        context: Context,
        message: String = "加载中...",
        cancelable: Boolean = true,
        cancelListener: DialogInterface.OnCancelListener? = null,
        autoClose: Boolean = true
    ) {
        // 确保在主线程执行UI操作
        if (Looper.myLooper() == Looper.getMainLooper()) {
            showLoadingInternal(context, message, cancelable, cancelListener, autoClose)
        } else {
            mainHandler.post {
                showLoadingInternal(context, message, cancelable, cancelListener, autoClose)
            }
        }
    }

    private fun showLoadingInternal(
        context: Context,
        message: String,
        cancelable: Boolean,
        cancelListener: DialogInterface.OnCancelListener?,
        autoClose: Boolean
    ) {
        try {
            // 移除之前的自动关闭任务
            mainHandler.removeCallbacks(autoCloseRunnable)

            // 如果对话框已经在显示，则先更新内容而不是重新创建
            if (loadingDialog != null && loadingDialog?.isShowing == true) {
                loadingDialog?.updateMessage(message)
            } else {
                // 创建并显示新的加载对话框
                loadingDialog = WidgetUtils.getLoadingDialog(context)
                    .setIconScale(0.4f)
                    .setLoadingSpeed(8)
                    .apply {
                        updateMessage(message)
                        setCancelable(cancelable)
                        setCanceledOnTouchOutside(cancelable)
                        setOnCancelListener(cancelListener)
                        show()
                    }
            }

            // 设置自动关闭
            if (autoClose) {
                mainHandler.postDelayed(autoCloseRunnable, AUTO_CLOSE_DELAY)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun hideLoading() {
        // 移除自动关闭任务
        mainHandler.removeCallbacks(autoCloseRunnable)

        if (Looper.myLooper() == Looper.getMainLooper()) {
            hideLoadingInternal()
        } else {
            mainHandler.post {
                hideLoadingInternal()
            }
        }
    }

    private fun hideLoadingInternal() {
        try {
            loadingDialog?.let { dialog ->
                if (dialog.isShowing) {
                    dialog.dismiss()
                }
            }
            loadingDialog = null
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun isLoading() = loadingDialog?.isShowing ?: false
}